package top.jojoace.JPA.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.jojoace.JPA.model_DDL.BothWay_OneToMany.BW_School;
import top.jojoace.JPA.model_DDL.BothWay_OneToMany.BW_StudentItem;
import top.jojoace.JPA.model_DDL.OneWay_OneToMany.OW_StudentItem;
import top.jojoace.JPA.model_DDL.SqlModel.SqlModel;
import top.jojoace.JPA.service.SchoolService;
import top.jojoace.JPA.service.SqlServiceImpl;
import top.jojoace.JPA.service.StudentService;

import java.util.Map;
import java.util.Optional;

@Controller
@ResponseBody
public class MyController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private SchoolService schoolService;

    @Autowired
    private SqlServiceImpl sqlService;

    @GetMapping("/bwstu/{id}")
    public Object getBWStudnet(@PathVariable(name = "id") Integer id){
        BW_StudentItem studentItem = this.studentService.selectBWStudentById(id);
        return Optional.ofNullable(studentItem).orElseGet(BW_StudentItem::new);
    }

    @GetMapping("/owstu/{id}")
    public Object getOWStudnet(@PathVariable(name = "id") Integer id){
        OW_StudentItem studentItem = this.studentService.selectOWStudentById(id);
        return Optional.ofNullable(studentItem).orElseGet(OW_StudentItem::new);
    }

    @GetMapping("/bwsch/{id}")
    public Object getSchool(@PathVariable(name = "id") Integer id){
        BW_School studentItem = this.schoolService.selectSchoolById(id);
        return studentItem;
    }

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @PostMapping("/save")
    public Map<String, Object> saveSqlModel(@RequestBody SqlModel sqlModel){
        return sqlService.saveSqlModel(sqlModel);
    }
}
